package cn.gxa.j229.bug.finance.service.borrowing.impl;

import cn.gxa.j229.bug.finance.mapper.InvestmentRecordMapper;
import cn.gxa.j229.bug.finance.pojo.InvestmentRecord;
import cn.gxa.j229.bug.finance.service.borrowing.InvestmentRecordService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author FanJiang
 * @Date 2020/9/25
 * @Version 1.0
 */

@Service("investmentRecordService")
public class InvestmentRecordServiceImpl implements InvestmentRecordService {

    @Resource
    InvestmentRecordMapper investmentRecordMapper;

    @Override
    public List<Map<String, Object>> findAll() {
        return investmentRecordMapper.findAll();
    }

    @Override
    public List findAllPage(InvestmentRecord investmentRecord) {
        investmentRecord.setLimit((investmentRecord.getLimit() - 1) * investmentRecord.getPage());
//        Integer startRow = (limit - 1) * page;
        return investmentRecordMapper.findAllPage(investmentRecord);
    }

    @Override
    public Integer countPage(InvestmentRecord investmentRecord) {
        return investmentRecordMapper.countPage(investmentRecord);
    }

    @Override
    public void export(XSSFWorkbook workbook) {
        //1.查询出所有的部门数据
        List<Map<String, Object>> investmentList = findAll();
        //2.创建XSSFWorkbook
//        XSSFWorkbook workbook = new XSSFWorkbook();
        //3.创建sheet
        XSSFSheet sheet = workbook.createSheet();
        //4.创建表头
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("序号");
        row.createCell(1).setCellValue("标的编号");
        row.createCell(2).setCellValue("投资人");
        row.createCell(3).setCellValue("投资人手机");
        row.createCell(4).setCellValue("标名");
        row.createCell(5).setCellValue("投资金额");
        row.createCell(6).setCellValue("利息");
        row.createCell(7).setCellValue("利息管理费");
        row.createCell(8).setCellValue("收益方式");
        row.createCell(9).setCellValue("已收金额");
        row.createCell(10).setCellValue("待收总额");
        row.createCell(11).setCellValue("投资渠道");
        row.createCell(12).setCellValue("投资时间");
        row.createCell(13).setCellValue("投资状态");
        //5.遍历集合填充每一行的数据
        for (int i = 0; i < investmentList.size(); i++) {
            Row r = sheet.createRow(i + 1);
            InvestmentRecord investmentRecord = new InvestmentRecord();
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
//                investmentRecord.setPhone(investmentList.get(i).get("phone").toString());
            //   InvestmentRecord investmentRecord = (InvestmentRecord) investmentList.get(i);
            r.createCell(0).setCellValue(investmentList.get(i).get("id").toString());
            r.createCell(1).setCellValue(investmentList.get(i).get("num").toString());
            r.createCell(2).setCellValue(investmentList.get(i).get("investor").toString());
            r.createCell(3).setCellValue(investmentList.get(i).get("phone").toString());
            r.createCell(4).setCellValue(investmentList.get(i).get("entitle").toString());
            r.createCell(5).setCellValue(investmentList.get(i).get("money").toString());
            r.createCell(6).setCellValue(investmentList.get(i).get("interest").toString());
            r.createCell(7).setCellValue(investmentList.get(i).get("managerfee").toString());
            r.createCell(8).setCellValue(investmentList.get(i).get("income").toString());
            r.createCell(9).setCellValue(investmentList.get(i).get("amount").toString());
            r.createCell(10).setCellValue(investmentList.get(i).get("duein").toString());
            r.createCell(11).setCellValue(investmentList.get(i).get("ditch").toString());
            //日期处理
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sDate = df.format(investmentList.get(i).get("investime"));
            r.createCell(12).setCellValue(sDate);
            r.createCell(13).setCellValue(investmentList.get(i).get("isname").toString());
        }
    }
}
